<?php namespace App\Http\Controllers\admin\tech;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Input;
use Validator;
use App\Models\Technologies;
use App\Models\Techs;
use App\Models\Domains;
use App\Models\TechDomain;
use App\Models\TechTech;
use App\Models\MainBody;
use App\Models\TechMainBody;
use App\Models\Users;

use App;
use Redirect;
use Session;
class TechController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */


  

	public function techsList()
	{
		$current = (int)Input::get("current");
		$rowCount = (int)Input::get("rowCount");
		$sort = Input::get("sort")["id"];
		$sort = $sort ? $sort : "desc";

		$keywords = Input::get('searchPhrase');

        $model= new Technologies;
		$count = $model->all()->count();

		if($keywords){
			$tech = $model->where("tech_name", "like", "%".$keywords."%")->orWhere("description", "like", "%".$keywords."%")->skip(($current - 1)*$rowCount)->take($rowCount)->get()->toArray();
		}else{
			$tech = $model->skip(($current - 1)*$rowCount)->take($rowCount)->get()->toArray();
		}

		return array(
			"current" => $current,
			"rowCount" => $rowCount,
			"rows" => $tech,
			"total" => $count
		);
	}



	public function index()
	{
		//
		if(Session::has('admin')){
		return view("admin.tech.index")->withIsTechs(true);
	    }else{
	    	return Redirect::to('admin/dologin');
	    }
	}

	public function editTech()
	{
		//
		$techs = Techs::all()->toArray();
		$domains = Domains::where("parent_id", 0)->get()->toArray();
		$mBody = MainBody::all()->toArray();
		return view("admin.tech.edit_tech", ["techs" => $techs, "domains" => $domains, "mBody" => $mBody])->withIsTechs(true);
	}


	public function getTech()
	{
		$tid = (int)Input::get("tid");
		$uid = $this->getUid();
		$model = Technologies::with(['techDomain', 'techTech.tech', 'techDomain.domain.parent', 'mainbody.mainbody'])->where("id", $tid);
		if($model->count()){
			$data = $model->get()[0]->toArray();
			return $this->json($data);
		}
	}


	public function updateTech()
	{
		$rule = [];
		$msg = [];
		$validator = Validator::make(Input::all(), $rule, $msg);
		if($validator->passes())
		{
			$tid = Input::get("tid");
			$model = Technologies::where("id", $tid);
			if($model->count())
			{
				$uid = Technologies::where("id", $tid)->pluck("uid");
				$data = Input::except("tid", "tech_id", "domain_id", "main_body");
				$imgArr = Input::only("image");
				$this->moveImages($imgArr, env("TECH_DIR"), $uid);
				$model->update($data);
				$techTech = TechTech::where("tech_id", $tid);
				$techTech->update(["cate_id" => Input::get("tech_id")]);
				$domain = TechDomain::where("tech_id", $tid);
				$domain->update(["cate_id" => Input::get("domain_id")]);
				$main_model = TechMainBody::where("tech_id", $tid);
				$main_model->delete();
				$device_main = Input::get("main_body");
				foreach ($device_main as $main)
				{
					TechMainBody::create(["mb_id" => $main, "tech_id" => $tid]);
				}

				return $this->json([]);
			}
		}
		else
		{
			$errors = $validator->messages();
			return self::error('',$errors->first());
		}
	}

	public function techAudit()
	{
		$tid = Input::get("tid");
		$state = Input::get("state");
		$model = Technologies::where("id", $tid);
		if($model->count())
		{
			$model->update(["verify_state" => $state]);
			return $this->json([]);
		}
	}



	public function addTech()
	{
		$techs = Techs::all()->toArray();
		$domains = Domains::where("parent_id", 0)->get()->toArray();
		$mBody = MainBody::all()->toArray();
		$user = Users::with(["devicetech"])->where("role_id", env("DEVICE_TECH"))->get()->toArray();
		return view("admin.tech.add_tech", ["techs" => $techs, "domains" => $domains, "mBody" => $mBody, "user" => $user])->withIsTechs(true);
	}


	public function saveTech()
	{

		$rule = [];
		$msg = [];
		$validator = Validator::make(Input::all(), $rule, $msg);
		if($validator->passes())
		{
			$uid = Input::get("user_id");
			$imgArr = Input::only("image");
			$this->moveImages($imgArr, env("TECH_DIR"), $uid);
			$data = Input::all();
			$data["uid"] = $uid;
			$tech = Technologies::create($data);
			$techTech = [
				"cate_id"	=>	Input::get("tech_id"),
				"tech_id"	=>	$tech->id
			];
			TechTech::create($techTech);
			$techDomain = ["cate_id" => Input::get("domain_id"), "tech_id" => $tech->id];
			TechDomain::create($techDomain);
			$device_main = Input::get("main_body");
			foreach ($device_main as $main)
			{
				TechMainBody::create(["mb_id" => $main, "tech_id" => $tech->id]);
			}

			return $this->json([]);
		}
		else
		{
			$errors = $validator->messages();
			return self::error('',$errors->first());
		}
	}
	// public function getTechAudit(){
 //  		$pid = Input::get("uid");
 //  		$state = Input::get("state");
 //  		$data = Devices::where("id", $did);
 //  		$data->update(["verify_state" => $state]);
 //  	}

	public function detailTech()
	{
		//
		return view("admin.tech.detail_tech")->withIsTechs(true);
	}

	public function techList()
	{

	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
